import React from 'react';
import {Button, Col, message, Tooltip, Row} from 'antd';
import {
    WeaDialog,
    WeaFormItem,
    WeaLocaleProvider,
    WeaSearchGroup,
    WeaInput,
    WeaBrowser,
    WeaDatePicker,
    WeaTimePicker,
    WeaSelect,
    WeaCheckbox, WeaRightMenu
} from 'ecCom';
import {WeaSwitch} from "comsMobx";

const getLabel = WeaLocaleProvider.getLabel;

/**
 * 绩效-考核代理
 */
export default class AccessAgentSave extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            form: {
                id: '',
                beagenterId: '',//被代理人
                agenterId: '',//代理人
                programtype: '',//方案类型
                beginDate: '',//代理开始日期
                beginTime: '',//代理开始时间
                endDate: '',//代理结束日期
                endTime: '',//代理结束时间
                issocre: '1',//是否代理评分
                scorehistory: '0',//是否能查看历史评分数据
                ischeck: '0',//是否代理评分审核
            },
            beagenterData: [],//被代理人数组
            agenterData: [],//代理人数组
        }
    }

    componentWillMount() {
        const {data, beagenterData} = this.props;
        this.setState({form: {...data}, beagenterData});
    }

    render() {
        const {show, onClose} = this.props;
        const {selectedKey} = this.state;
        return (
            <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@hc28za`}
                icon='icon-coms-implement'
                iconBgcolor='rgb(150, 53, 138)'
                style={{width: 700, height: 500}}
                buttons={[
                    <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@u23iro@save`} type="primary" onClick={this.doSave}>{getLabel(86, '保存')}</Button>,
                    <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@913ue7@cancel`} type="ghost" onClick={onClose}>{getLabel('31129', '取消')}</Button>
                ]}
                title={getLabel('519370', '考核代理')}
                visible={show}
                onCancel={onClose}
                destroyBodyOnClose={true}
            >
                <WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@nn3jix`}
                    title={getLabel('1361', '基本信息')}
                    needTigger={false}
                    col={1}
                    showGroup={true}
                    items={this.getItems()}/>
            </WeaDialog>
        )
    }

    //获取方案类型
    getProgramType = () => {
        return [
            {key: "1", showname: getLabel('19398', '月度')},
            {key: "2", showname: getLabel('17495', '季度')},
            {key: "3", showname: getLabel('20729', '半年')},
            {key: "4", showname: getLabel('17138', '年度')},
        ];
    }

    onFromChange = (params) => {
        let {form} = this.state;
        for (let key in params) {
            form[key] = params[key]
            this.setState({form})
        }
    }

    getItems = () => {
        const {selectedKey, form, beagenterData, agenterData} = this.state;
        const {isAdd} = this.props;
        let date = new Date();
        return [
            {
                com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@58ln16@principal`}
                    label={getLabel('17565', '被代理人')}
                    labelCol={{span: 6}}
                    wrapperCol={{span: 6}}
                >
                    <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@hkp216@principal`}
                        type={17}
                        isSingle={true}
                        viewAttr={1}
                        style={{lineHeight: '30px'}}
                        title={getLabel('179', '人力资源')}
                        replaceDatas={beagenterData}
                    />
                </WeaFormItem>)
            }, {
                com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@vk89ya@hrmid`}
                    label={getLabel('17566', '代理人')}
                    labelCol={{span: 6}}
                    wrapperCol={{span: 6}}
                >
                    {isAdd ? <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@amsb3j@hrmid`}
                        type={17}
                        isSingle={true}
                        viewAttr={3}
                        title={getLabel('179', '人力资源')}
                        replaceDatas={agenterData}
                        onChange={(ids, names, datas) => {
                            this.setState({agenterData: datas});
                            this.onFromChange({agenterId: ids});
                        }}
                    /> : <span dangerouslySetInnerHTML={{__html: form.agenterIdspan}}/>
                    }
                </WeaFormItem>)
            },
            {
                com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@d7s4gh@startdate`}
                    label={getLabel('519372', '开始日期,时间')}
                    labelCol={{span: 6}}
                    wrapperCol={{span: 18}}
                >
                    <WeaDatePicker ecId={`${this && this.props && this.props.ecId || ''}_WeaDatePicker@8j0tec@startdate`}
                        value={form.beginDate}
                        noInput
                        startValue={date}
                        onChange={(val) => this.onFromChange({beginDate: val})}
                    />
                    <WeaTimePicker ecId={`${this && this.props && this.props.ecId || ''}_WeaTimePicker@hu3mcm@starttime`}
                        style={{paddingLeft: "18px"}}
                        value={form.beginTime}
                        noInput
                        onChange={(val) => this.onFromChange({beginTime: val})}
                    />
                </WeaFormItem>)
            },
            {
                com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@r4ssps@enddate`}
                    label={getLabel('519373', '结束日期,时间')}
                    labelCol={{span: 6}}
                    wrapperCol={{span: 18}}
                >
                    <WeaDatePicker ecId={`${this && this.props && this.props.ecId || ''}_WeaDatePicker@rn7lf4@enddate`}
                        value={form.endDate}
                        noInput
                        startValue={date}
                        onChange={(val) => this.onFromChange({endDate: val})}
                    />
                    <WeaTimePicker ecId={`${this && this.props && this.props.ecId || ''}_WeaTimePicker@i5y979@endtime`}
                        style={{paddingLeft: "18px"}}
                        value={form.endTime}
                        noInput
                        onChange={(val) => this.onFromChange({endTime: val})}
                    />
                </WeaFormItem>)
            }, {
                com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@0hext9@agentscore`}
                    labelCol={{span: 6}}
                    wrapperCol={{span: 18}}
                    label={getLabel(521094, '代理绩效评分')}
                >
                    <WeaCheckbox ecId={`${this && this.props && this.props.ecId || ''}_WeaCheckbox@idni5p@agentscore`}
                        value={form.isscore}
                        display="switch"
                        onChange={(val) => this.onFromChange({isscore: val})}
                    />
                </WeaFormItem>)
            }, {
                com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@x82efm@agentaduit`}
                    label={getLabel(521095, '代理绩效审批')}
                    labelCol={{span: 6}}
                    wrapperCol={{span: 6}}
                >
                    <WeaCheckbox ecId={`${this && this.props && this.props.ecId || ''}_WeaCheckbox@dn7afk@agentaduit`}
                        value={form.ischeck}
                        display="switch"
                        onChange={(val) => this.onFromChange({ischeck: val})}
                    />
                </WeaFormItem>)
            }, {
                com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@3mnzij@viewhistory`}
                    label={getLabel(521096, '考核历史查看')}
                    labelCol={{span: 6}}
                    wrapperCol={{span: 6}}
                >
                    <WeaCheckbox ecId={`${this && this.props && this.props.ecId || ''}_WeaCheckbox@iijdur@viewhistory`}
                        value={form.scorehistory}
                        display="switch"
                        onChange={(val) => this.onFromChange({scorehistory: val})}
                    />
                </WeaFormItem>)
            }, {
                com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@54cha1@agenttype`}
                    label={getLabel('520809', '代理考核类型')}
                    labelCol={{span: 6}}
                    wrapperCol={{span: 6}}
                >
                    <WeaSelect ecId={`${this && this.props && this.props.ecId || ''}_WeaSelect@ca3cv1@agenttype`}
                        multiple
                        viewAttr={3}
                        value={form.programtype}
                        options={this.getProgramType()}
                        onChange={(val) => this.onFromChange({programtype: val})}
                    />
                </WeaFormItem>)
            }
        ]
    }

    //执行保存
    doSave = () => {
        const {form} = this.state;
        const {save, userid} = this.props;

        if (!form.agenterId) {
            message.warning(getLabel('519375', '代理人必须填写'));
            return;
        }

        if (userid == form.agenterId) {
            message.warning(getLabel('519376', '被代理人和代理人不能相同'));
            return;
        }

        var beginDate, endDate;
        if (form.beginDate) {
            if (form.beginTime) {
                beginDate = new Date(form.beginDate + " " + form.beginTime);
            } else {
                beginDate = new Date(form.beginDate);
            }
        } else if (form.beginTime) {
            message.warning(getLabel('127684', '设置开始时间必须设置开始日期'));
            return;
        }
        if (form.endDate) {
            if (form.endTime) {
                endDate = new Date(form.endDate + " " + form.endTime);
            } else {
                endDate = new Date(form.endDate);
            }
        } else if (form.endTime) {
            message.warning(getLabel('127685', '设置结束时间必须设置结束日期'));
            return;
        }
        if (beginDate && endDate && beginDate > endDate) {
            message.warning(getLabel('127683', '结束时间不能小于等于开始时间'));
            return;
        }
        if (!form.programtype) {
            message.warning(getLabel('521182','代理考核类型不能为空') );
            return;
        }
        save({
            operation: 'addAgent',
            ...form
        });
    }

};
